package com.huluobo.a2205monthdemo.parking

import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2205monthdemo.R
import com.huluobo.a2205monthdemo.adapter.ParkingAdapter
import com.huluobo.a2205monthdemo.databinding.ActivityParkingActivityBinding
import com.huluobo.base.bean.ParkingBean
import com.huluobo.base.bean.ParkingBeanItem
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class ParkingActivity : BaseActivity<ActivityParkingActivityBinding, ParkingViewModel>() {
    private lateinit var parkingAdapter: ParkingAdapter
    private lateinit var parkingBeanItem: ParkingBeanItem
    override fun initData() {
        vm.getAllParking()
        vm.getAllParkingSuccess.observe(this) {
            val list = Gson().fromJson(it, ParkingBean::class.java)
            parkingAdapter.data.clear()
            parkingAdapter.data.addAll(list)
            parkingAdapter.notifyDataSetChanged()
        }

        vm.deleteParkingSuccess.observe(this) {
            ToastUtils.showLong("删除成功")
            parkingAdapter.remove(parkingBeanItem)
        }
    }

    override fun initView() {
        binding.parkingHeader.headerTitle.text = "车位申请"
        binding.parkingHeader.headerBack.visibility = View.VISIBLE
        binding.parkingHeader.headerBack.setOnClickListener { finish() }

        binding.parkingRv.layoutManager = LinearLayoutManager(this)
        parkingAdapter = ParkingAdapter()
        binding.parkingRv.adapter = parkingAdapter

        parkingAdapter.setOnItemClickListener { adapter, view, position ->
            val parkingBeanItem = parkingAdapter.data[position]
            val intent = Intent(this, ParkingVRActivity::class.java)
            intent.putExtra("parkingBean", parkingBeanItem)
            startActivity(intent)
        }

        parkingAdapter.setOnItemLongClickListener { adapter, view, position ->
            AlertDialog.Builder(this)
                .setTitle("删除车位信息")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", object : OnClickListener {
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        parkingBeanItem = parkingAdapter.data[position]
                        val map = mutableMapOf<String, Int>()
                        map[Const.PARAM_PARKING_ID] = parkingAdapter.data[position].parkId

                        val json = Gson().toJson(map)
                        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                        vm.deleteParking(body)
                    }
                })
                .show()
            true
        }
    }
}